#include "SmTest.h"

#include <windows.h>
#include "GlCamera.h"
#include "GlPointLight.h"
#include "GlSpotLight.h"
#include "GlSprite.h"
#include "GlText.h"
#include "Utility.h"

namespace SneakingMission
{
	TestGame *TestGame::msSingleton(0);
	//-------------------------------------------
	TestGame::~TestGame()
	{
		if(mSpotLight)
			delete mSpotLight;
		if(mPointLight)
			delete mPointLight;
	}

	//-------------------------------------------
	void TestGame::init()
	{
		mSpotLight = GraphicLibrary::SpotLight::Create(2.0f * Utility::Pi / 3.0f, 200.0f);
		mPointLight = new GraphicLibrary::PointLight(200.0f);
		mPointLight->setVisibility(true);
		mSpotLight->setVisibility(false);
		mPointLight->move(GraphicLibrary::Position(3.0f, 37.000008f));
		mPointLight->setColor(1.0f, 0.0f, 0.0f, 1.0f);
		GraphicLibrary::Sprite::Create("Enemy")->move(GraphicLibrary::Position(100.0f, 0.0f));
		GraphicLibrary::Sprite::Create("Enemy")->move(GraphicLibrary::Position(150.0f, 0.0f));
		GraphicLibrary::Sprite::Create("Ninja")->move(GraphicLibrary::Position(-310.0f, 400.0f));
		GraphicLibrary::Sprite::Create("NewGameButton")->move(GraphicLibrary::Position(-300.0f, -400.0f));
		GraphicLibrary::Sprite::Create("HighScoresButton")->move(GraphicLibrary::Position(100.0f, -400.0f));
		GraphicLibrary::Text *text = new GraphicLibrary::Text("TestFont/", "chdaefgbi");
		text->move(GraphicLibrary::Position(200.0f, 100.0f));
		text->setScale(GraphicLibrary::Size(1.0f, 3.0f));
		text->setText("chdaefgbiaefgb");
		GraphicLibrary::GraphicComponent::msSaturation = 0.0f;
	}

	//-------------------------------------------
	bool TestGame::tick(const float deltaTime)
	{
		if(mRotateLeft)
			mSpotLightDirection +=  deltaTime * 2.0f;
		if(mRotateRight)
			mSpotLightDirection -=  deltaTime * 2.0f;
		mSpotLight->setDirection(mSpotLightDirection);
		return false;
	}

	//-------------------------------------------
	void TestGame::mouseDown(const float x, const float y)
	{
		mMouseDown = true;
		mouseMove(x, y);
	}

	//-------------------------------------------
	void TestGame::mouseUp(const float x, const float y)
	{
		mouseMove(x, y);
		mMouseDown = false;
	}

	//-------------------------------------------
	void TestGame::mouseMove(const float x, const float y)
	{
		if(mMouseDown)
		{
			mSpotLight->move(GraphicLibrary::Camera::GetSingleton()->projectScreenSpaceToGameSpace(x, y));
			mPointLight->move(GraphicLibrary::Camera::GetSingleton()->projectScreenSpaceToGameSpace(x, y));
		}
	}

	//-------------------------------------------
	void TestGame::keyPressed(const unsigned int key)
	{
		switch(key)
		{
			case VK_LEFT :
				mRotateLeft = !mRotateLeft;
				break;
			case VK_RIGHT :
				mRotateRight = !mRotateRight;
				break;
			case VK_SPACE :
				mSpotLight->setVisibility(!mSpotLight->isVisible());
				mPointLight->setVisibility(!mPointLight->isVisible());
				break;
		}
	}

	//-------------------------------------------
	TestGame::TestGame()
		: mSpotLightDirection(0.0f)
		, mSpotLight(0)
		, mPointLight(0)
		, mMouseDown(false)
		, mRotateLeft(false)
		, mRotateRight(false)
	{
	}
}
